package cn.gov.msa.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.gov.msa.collection.SharedLibraryService;
import cn.gov.msa.model.BaseShip;
import cn.gov.msa.service.BaseShipService;
import cn.gov.msa.service.VerifyService;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/share", produces = "text/plain;charset=UTF-8")
public class ShareController {
	@Autowired
	VerifyService verifyService;
	@Autowired
	BaseShipService baseShipService;
	
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	public String check(@RequestBody JSONObject param) throws Exception { 
        String result = SharedLibraryService.check(JSONObject.fromObject(param));     
        return result;  
    } 
	
	@RequestMapping(value = "/getverify", method = RequestMethod.POST)
	public String getVerify(@RequestBody JSONObject param) throws Exception {     
        String result = this.verifyService.getVerifyList(param.toString());
        
        return result;  
    } 
	
	@RequestMapping(value = "/addLocalShip", method = RequestMethod.POST)
	public String addLocalShip(@RequestBody JSONObject param) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_save_no;
		JSONObject jsonResult = new JSONObject();

		try {
			if(!"".equals(param.optString("sysorg"))){
				BaseShip ship = (BaseShip) JSONObject.toBean(param, BaseShip.class);
				result = baseShipService.insertSelective(ship);
				if(result > 0){
					info = InfoObjUtil.Success_save_OK;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	} 
	
	@RequestMapping(value = "/getLocalShip", method = RequestMethod.POST)
	public String getLocalShip(@RequestBody JSONObject param) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		
		try {
			if(!"".equals(param.optString("sysorg"))){
				BaseShip ship = (BaseShip) JSONObject.toBean(param, BaseShip.class);
				List<BaseShip> list = baseShipService.selectData(ship);
				if(list != null && list.size() > 0){
					result = 1;
					info = InfoObjUtil.Success_select_OK;
					jsonResult.put("datas", JSONObject.fromObject(list.get(0)));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		
		return jsonResult.toString();
	} 
}
